/*
 * @Date: 2023-07-31 14:01:38
 * @LastEditors: lijingang
 * @LastEditTime: 2023-08-30 14:06:54
 * @FilePath: /ds-brain-web/src/views/login/mixin/login.js
 */
import { encryption } from '@/utils/util'
import { encryptionKey } from '@/settings'
import { LOGIN_TYPE } from '../configuration'

export const loginMixin = {
  created: function () { },
  methods: {
    /**
     * @description 登录
     */
    loginExe() {
      const user = encryption({
        data: this.form,
        key: encryptionKey,
        param: ['password', 'username']
      })

      const { username, password } = user

      const param = {
        loginType: LOGIN_TYPE[this.options.tabKey],
        loginTab: this.options.loginTab,
        terminalId: this.terminalId,
        captchaCode: this.form.code,
        account: username,
        password: password,
        domain: this.domain,
        randomStr: this.form.randomStr,
        appCode: this.appCode
      }

      // 兼容绑定手机号
      if (this.unionId) {
        param['account'] = this.unionId
      }

      // 兼容绑定手机号
      if (this.openId) {
        param['openId'] = this.openId
      }

      // 存在短信验证码的情况
      if (this.form.verificationCode) {
        param['verifyCode'] = this.form.verificationCode
        param['verifyCodeId'] = this.form.verifyCodeId
        param['verifyCodeType'] = this.form.verificationCodeType
      }
      this.loading = true

      this.$store
        .dispatch('user/login', param)
        .then(async () => {
          if (this.$route.query.redirect) {
            this.$router.replace(this.$route.query.redirect)
          } else {
            this.$router.push({ path: '/' })
          }
        })
        .catch(() => {
          this.$refs.childimg.refreshCode()
        })
        .finally(() => {
          this.loading = false
        })
    }
  }
}
